package com.probiz.estore.imports.handler.catalog.productCategory;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.probiz.estore.catalog.service.ProductCategoryManager;
import com.probiz.estore.common.model.catalog.ProductCategory;
import com.probiz.estore.common.model.product.ProductType;
import com.probiz.estore.imports.handler.ColumnHandler;
import com.probiz.estore.imports.handler.basic.ColumnBasicHandler;
import com.probiz.estore.imports.model.Column;
import com.probiz.estore.imports.model.ImportModel;
import com.probiz.estore.product.service.ProductTypeManager;

public class ProductTypeHandler extends ColumnBasicHandler implements ColumnHandler{
	private Logger logger = Logger.getLogger(ProductTypeHandler.class);
	protected ProductCategoryManager	productCategoryManager	= null;
	protected ProductTypeManager	productTypeManager	= null;
	public void setProductCategoryManager(ProductCategoryManager productCategoryManager) {
		this.productCategoryManager = productCategoryManager;
	}

	public void setProductTypeManager(ProductTypeManager productTypeManager) {
		this.productTypeManager = productTypeManager;
	}

	public void setProperty(ImportModel importModel, Column column)	throws Exception {
		ProductCategory category=(ProductCategory)importModel.getTarget();
		String value = column.getValue();
		if(StringUtils.isNotBlank(value)){
			List<ProductType> productTypeList = productTypeManager.listByProperty("productTypeName", value.trim());
			if(0 == productTypeList.size()){
				//设置结果
				importModel.setResult(ImportModel.RESULT_ERROR);
				String msg = "无法导入，因为找不到关联商品类型【"+value+"】";
				logger.error(msg);
				importModel.setMsg(msg);
				return;
			}else if(1 == productTypeList.size()){
				category.setProductType(productTypeList.get(0));
			}else{
				//设置结果
				importModel.setResult(ImportModel.RESULT_ERROR);
				String msg = "无法导入，因为找到多个关联商品类型【"+value+"】，系统无法识别";
				logger.error(msg);
				importModel.setMsg(msg);
			}
		}
		
		//设置结果
		importModel.setResult(ImportModel.RESULT_SUCCESS);
	}
	

}
